package com.party.service.excellentparty.impl;

import com.party.common.Page;
import com.party.mapper.VotePersonMapper;
import com.party.model.VoPtionExample;
import com.party.model.VotePerson;
import com.party.model.VotePersonExample;
import com.party.service.excellentparty.VotePersonService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class VotePersonServiceImpl implements VotePersonService {

    @Resource
    VotePersonMapper votePersonMapper;

    @Override
    public int addVotePerson(VotePerson votePerson) {
        int num = votePersonMapper.insertSelective(votePerson);
        return num;
    }

    @Override
    public int deleteVotePersonByPrimaryKey(Integer id) {
        int num = votePersonMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteVotePersonByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += votePersonMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public VotePerson getVotePersonByPrimaryKey(Integer id) {
        VotePerson votePerson = votePersonMapper.selectByPrimaryKey(id);
        return votePerson;
    }

    @Override
    public VotePerson getVotePerson(VotePerson votePerson) {
        VotePersonExample votePersonExample = new VotePersonExample();
        VotePersonExample.Criteria criteria=votePersonExample.createCriteria();
        criteria.andUidEqualTo(votePerson.getUid());
        criteria.andVoteidEqualTo(votePerson.getVoteid());
        List<VotePerson> votePersonList=votePersonMapper.selectByExample(votePersonExample);

        if(votePersonList!=null&&votePersonList.size()>0){
            return votePersonList.get(0);
        }else{
            return null;
        }
    }

    @Override
    public int updateVotePersonByPrimaryKey(VotePerson votePerson) {
        int num = votePersonMapper.updateByPrimaryKeySelective(votePerson);
        return num;
    }

    @Override
    public VotePerson getVotePersonByUid(VotePerson votePerson) {
        VotePerson votePerson1=votePersonMapper.getVotePersonByUid(votePerson);
        return votePerson1;
    }

    @Override
    public Page getVotePersonList(VotePerson votePerson, int pageSize, int pageNumber) {
        VotePersonExample votePersonExample = new VotePersonExample();
        /**
         * 可以添加查询条件
         */
        if(votePerson != null)
        {
            VotePersonExample.Criteria criteria = votePersonExample.createCriteria();

//            if(votePerson.getType() != null && !"".equals(votePerson.getType()))
//            {
//                criteria.andTypeEqualTo(votePerson.getType());
//            }

        }
        votePersonExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = votePersonMapper.countByExample(votePersonExample);
        //获取指定的页码数据
        votePersonExample.setBeginNum((pageNumber - 1) * pageSize);
        votePersonExample.setEndNum(pageSize);
        List<VotePerson> list = votePersonMapper.getListPage(votePersonExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public int addVotePersonByUserIds(Integer voteid,String[] userids) {
        int num=0;
        if(!"".equals(userids)&&userids!=null){
            VotePersonExample votePersonExampleDelete = new VotePersonExample();
            VotePersonExample.Criteria criteriaDelete=votePersonExampleDelete.createCriteria();
            criteriaDelete.andVoteidEqualTo(voteid);
            votePersonMapper.deleteByExample(votePersonExampleDelete);
            for (String uid : userids) {
                VotePerson votePerson=new VotePerson();
                votePerson.setVoteid(voteid);
                Integer uidInt=Integer.parseInt(uid);
                votePerson.setUid(uidInt);
//                VotePersonExample votePersonExample=new VotePersonExample();
//                VotePersonExample.Criteria criteria=votePersonExample.createCriteria();
//                criteria.andUidEqualTo(uidInt);
//                List<VotePerson> list=votePersonMapper.selectByExample(votePersonExample);
//                if(list.size()==0){
                    num += votePersonMapper.insertSelective(votePerson);
//                }
                num +=1;
            }
        }
        return num;
    }

    @Override
    public List<VotePerson> getAllVotePerson() {
        VotePersonExample votePersonExample = new VotePersonExample();
        List<VotePerson> list=this.votePersonMapper.getAllVotePerson(votePersonExample);
        return list;
    }

    public Page getListPageByCustorm(VotePerson votePerson, int pageSize, int pageNumber){

        //获取总的记录数量
        int count = votePersonMapper.countByVotePerson(votePerson);
        //获取指定的页码数据
        votePerson.setBeginNum((pageNumber - 1) * pageSize);
        votePerson.setEndNum(pageSize);
        List<VotePerson> list = votePersonMapper.getListPageByCustorm(votePerson);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public List<VotePerson> getAllUsersByVoteId(Integer voteid) {
        VotePerson votePerson=new VotePerson();
        votePerson.setVoteid(voteid);
        List<VotePerson> list = this.votePersonMapper.getAllVotePersonByVoteId(votePerson);
        return list;
    }
}
